<?php
namespace app\api\model;

use app\common\helper\Obj2Array;
use app\common\model\data\SystemMusicSpiderModel;
use app\common\model\project_model\SystemMusicCategoryModel;
use app\common\model\project_model\SystemMusicModel as SystemMusic;

class SystemMusicModel extends SystemMusic {

    public static $err_msg;

    /**
     * 获取音乐类型及所有的音乐
     *
     * @param null $keyword
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function musicCategory($keyword = null) {
        $cate = SystemMusicCategoryModel::categoryList();
       $SystemMusicM = new self();

       if ($keyword) {
           $SystemMusic = $SystemMusicM
               ->field('id, mp3_url, type, title, singer, music_id')
               ->where('title|singer', 'like', '%' . $keyword . '%')
               ->where(['status' => self::STATUS_OK])->order(['sort' => 'ASC'])->select();
       } else {
           $SystemMusic = $SystemMusicM->field('id, mp3_url, type, title, singer, music_id')->where(['status' => self::STATUS_OK])->order(['sort' => 'ASC'])->select();
       }
       if (!empty($SystemMusic)) {
           $SystemMusic = Obj2Array::resObject2Array($SystemMusic);
       }

       $data = [];
       $data['cate'] = $cate;
       $data['album_music'] = $SystemMusic;
       return $data;
    }

    /**
     * 通过cate_id获取分类下的所有音乐
     * @param $id
     * @return array|false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function musicByCateId($id) {
        $musicM = new self();
        $music = $musicM
            ->field('id, mp3_url, title, singer, type')
            ->where(['status' => self::STATUS_OK, 'cate_id' => $id])
            ->order(['sort' => 'ASC'])
            ->select();
        if (!empty($music)) {
            $music = Obj2Array::resObject2Array($music);
        }

        return $music;
    }

    /**
     * @param $id
     * @return array|false| \PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function musicById($id) {
        $musicM = new self();
        $music = $musicM->field('sort, created_time, updated_time, status', true)->where(['id' => $id])->find();
        $musicM->where(['id' => $id])->setInc('request_num', 1);
        return $music;
    }

    /**
     * 播放请求
     * @param $mid
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function requestPlay($mid) {
        $res = SystemMusicSpiderModel::getQQPlayUrl($mid);
        if (!$res) {
            self::$err_msg = SystemMusicSpiderModel::$err_msg;
            return false;
        }
        return $res;
    }
}